package fr.afcepf.atod12.projet1.dao.implementations;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import fr.afcepf.atod12.projet1.dao.interfaces.IDaoPersonne;
import fr.afcepf.atod12.projet1.entities.Medecin;
import fr.afcepf.atod12.projet1.entities.Patient;


@Service("daoPersonne")
@Transactional
public class DaoPersonneImpl implements IDaoPersonne {

	@PersistenceContext(unitName="myPersistenceUnit")
	private EntityManager entityManager;


	@SuppressWarnings("unchecked")
	@Override
	public List<Patient> getAllPatient() {
		Query query = entityManager.createQuery("select distinct p from Patient p left join fetch p.evenements");
		List<Patient> result = query.getResultList();
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Medecin> getAllMedecin() {
		Query query = entityManager.createQuery("select distinct m from Medecin m left join fetch m.evenements");
		List<Medecin> result = query.getResultList();
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Patient> getPatientByName(String name) {
		Query query = entityManager.createQuery("select distinct p from Patient p left join fetch p.evenements where p.nom like ?");
		query.setParameter(1,"%"+name+"%");
		List<Patient> result = query.getResultList();
		return result;
	}
	@Override
	public Patient getOnePatientByName(String name) {
		Query query = entityManager.createQuery("select p from Patient p where p.nom=:name");
		query.setParameter("name", name);
		try {
			return (Patient)query.getSingleResult();
		} catch (Exception e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Medecin> getMedecinByName(String name) {
		Query query = entityManager.createQuery("select distinct m from Medecin m left join fetch m.evenements where m.nom like ?");
		query.setParameter(1, "%"+name+"%");
		List<Medecin> result = query.getResultList();
		return result;
	}

	@Override
	public Medecin getOneMedecinByName(String name) {
		Query query = entityManager.createQuery("select m from Medecin m where m.nom=:name");
		query.setParameter("name", name);
		try {
			return (Medecin)query.getSingleResult();
		} catch (Exception e) {
			return null;
		}
	}
	@Override
	public Patient getPatientById(Integer idPatient) {
		Patient patient  = (Patient) entityManager.find(Patient.class, idPatient);
		patient.getEvenements().size();
		return patient;
	}

	@Override
	public Medecin getMedecinById(Integer idMedecin) {
		Medecin medecin  = (Medecin) entityManager.find(Medecin.class, idMedecin);
		medecin.getEvenements().size();
		return medecin;
	}

	@Override
	public List<Patient> getPatientByEvenement(Date date) {
//		Query query = entityManager.createQuery("from Personne p join fetch p.evenementsPatient where p.date like ?");
//		query.setParameter(1, date);
//		List<Personne> result = query.getResultList();
//		return result;
		return null;
	}


	@Override
	public boolean createPatient(Patient patient) {
		boolean retour = false;
		entityManager.persist(patient);
		Patient pat = (Patient) entityManager.find(Patient.class, patient.getIdPatient());
		if(pat != null)
			retour = true;
		return retour;
	}

	@Override
	public boolean deletePatient(Patient patient) {
		boolean retour = false;
		entityManager.remove(patient);
		Patient pat = (Patient) entityManager.find(Patient.class, patient.getIdPatient());
		if(pat == null)
			retour = true;
		return retour;
	}

	@Override
	public boolean updatePatient(Patient patient) {
		boolean retour = true;
		entityManager.merge(patient);
		return retour;
	}

	@Override
	public boolean createMedecin(Medecin medecin) {
		boolean retour = false;
		entityManager.persist(medecin);
		Medecin med = (Medecin) entityManager.find(Medecin.class, medecin.getIdMedecin());
		if(med != null)
			retour = true;
		return retour;
	}

	@Override
	public boolean deleteMedecin(Medecin medecin) {
		boolean retour = false;
		entityManager.remove(medecin);
		Medecin med = (Medecin) entityManager.find(Medecin.class, medecin.getIdMedecin());
		if(med == null)
			retour = true;
		return retour;
	}

	@Override
	public boolean updateMedecin(Medecin medecin) {
		boolean retour = true;
		entityManager.merge(medecin);
		return retour;
	}

}